<?php
namespace app\api\controller;

use think\Controller;
use think\Db;

class Weixin extends Controller
{
	public function get_token()
	{
		if(cache('token')){
			return cache('token');
		}

		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbac82045a637c02e&secret=7c320fea54e77a01fcc59ff7060b04e2";

		$data  = file_get_contents($url);

		$data = json_decode($data,true);

		cache("token",$data['access_token'],7200);

		return cache('token');
		
	}

	public function test()
	{
		$token = $this->get_token();

		var_dump($token);
	}

	public function getmenu()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->get_token();


		$data = file_get_contents($url);

		var_dump(json_encode(json_decode($data,true)['menu']));
	}

	public function setmenu()
	{

		$url  = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->get_token();
		$data = [
			'button'=>[
				[
					'type'=>'view',
					'name'=>'中国好声音',
					'url'=>'http://www.baidu.com'
				],
				[
					'type'=>'view',
					'name'=>'社区',
					'url'=>'http://1o533n8683.imwork.net'
				],
				[
					'name'=>'个人中心',
					'sub_button'=>[
						[
							'type'=>'view',
							'name'=>'头像修改',
							'url'=>'http://1o533n8683.imwork.net/index/user/set.html'
						],
						[
							'type'=>'view',
							'name'=>'密码',
							'url'=>'http://www.qq.com'
						],
						[
							'type'=>'view',
							'name'=>'中国声音2',
							'url'=>'http://www.163.com'
						]

					]

				],
			]
		];

		$this->post($url,$data);
	}

	public function getUser()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->get_token();


		$data = file_get_contents($url);

		$data = json_decode($data,true);

		return $data['data']['openid'];
	}


	public function send()
	{

		$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$this->get_token();

		$userList = $this->getUser();

		var_dump($userList);

		$data = [
			'touser'=>$userList,
			'msgtype'=>'text',
			'text'=>[
				'content'=>"大家好，今天晚上一起吃鸡!"
			]
		];

		$this->post($url,$data);

	}

	public function moban()
	{
		
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->get_token();


		$data = [
			'touser'=>"oF5Z2v5yzYfsCYP2oEDfz4ns94p0",
			'template_id'=>'xKMmLq3iJ5T1C7d1bKCr1XDYeOUJU1Xxo-MzRFFrzZc',
			'url'=>'http://1o533n8683.imwork.net/jie/1.html',
			'data'=>[
				'name'=>['value'=>'小明','color'=>'#ED1C24'],
				'kname'=>['value'=>'PHP实战班','color'=>'#ED1C24'],
				'time'=>['value'=>date("Y-m-d H:i:s"),'color'=>'#ED1C24'],
				'money'=>['value'=>'10.89','color'=>'#173177'],
				'extime'=>['value'=>'2019-03-01 0:0:0','color'=>'#173177'],
			]
		];

		$this->post($url,$data);
	}

	public function post($url,$data)
	{

		$curl = curl_init();

		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//post
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data,JSON_UNESCAPED_UNICODE));//post 数据 
		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen(json_encode($data,JSON_UNESCAPED_UNICODE))
		));
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, false);

		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

		$data = curl_exec($curl);

		var_dump($data);

		var_dump(json_decode($data,true));
	}
}